﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;
using BLL;
using Model;
using System.Data;
//using System.ComponentModel
//using System.Drawing
//using System.Linq
//using System.Text
//using System.Net


namespace NCRE学生考试端V1._0
{
    public partial class frmSingleStu : Form
    {
        public frmSingleStu()
        {
            InitializeComponent();
        }

        #region 窗体单一显示-李金颖-2018-12-8 11:38:25
        private static frmSingleStu FrmSingleStu = null;
        public static frmSingleStu GetInstance()
        {
            if (FrmSingleStu==null||FrmSingleStu.IsDisposed)
            {
                FrmSingleStu = new frmSingleStu();
            }
            return FrmSingleStu;
        }
        #endregion

        #region 实例化B层各类 修改人：李金颖-整理-2018-12-16 10:04:58
        //定义一个学生分数的逻辑类
        //StudentScoreBLL studentscorebll = new StudentScoreBLL()

        //定义一个学生的实体用来传递学生信息
        //StudentInfoEntity studentinfo = new StudentInfoEntity()

        //定义一个ExamConfigBLL类
        ExamConfigBll examconfigbll = new ExamConfigBll();

        //定义一个WordQuestionEntityBLL类
        WordQuestionEntityBll wordquestionbll = new WordQuestionEntityBll();

        //定义一个WinQuestionEntityBLL类
        WinQuestionEntityBll winquestionbll = new WinQuestionEntityBll();

        //定义一个ExcelQuestionEntityBLL类
        ExcelEntityBll excelQuestionBll = new ExcelEntityBll();

        //定义一个IEBLL类
        IeQuestionEntityBll iequestionbll = new IeQuestionEntityBll();

        //定义一个StudentBindPaperTypeBLL类
        StudentBindPaperTypeBLL studentBindPaperType = new StudentBindPaperTypeBLL();

        //定义一个PPTBLL类
        PptQuestionEntityBLL pptQuestionBll = new PptQuestionEntityBLL();

        //定义一个学生类
        StudentInfoBll studentinfoBll = new StudentInfoBll();

        //定义一个日志类
        LogBll logBll = new LogBll();
        #endregion

        #region 单个学生配题 修改人：李金颖-添加备注-2018-12-16 10:14:35
        /// <summary>
        /// 配题
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnSingleStu_Click(object sender, EventArgs e)
        {

            String studentID = SingleStudentConfigBox.Text.Trim();
            //判断非空
            if (studentID == "" || txtPaperType.Text.Trim() == "")
            {
                MessageBox.Show("请输入考生号或试卷号");
                return;
            }
            //验证学生是否可以考试
            StudentInfoBll studentBll = new StudentInfoBll();
            StudentInfoEntity enStudent = studentBll.GetStudentById(studentID);
            if (enStudent == null)
            {
                MessageBox.Show("学生不存在！");
                return;
            }
            //UserEntityBll userDal = new UserEntityBll()
            //this.Hide()
            //登录后，更改考生是否登录过标志-韩梦甜-2015-12-14

            //时间戳
            string timetamp = "";
            //判分完了之后插入的

            int d = 0;

            StudentBindTypeEntityBll studentbindtypeentitybll = new StudentBindTypeEntityBll();
            StudentBindPaperTypeEntity studentbindinfo = new StudentBindPaperTypeEntity() { StudentID = studentID };

            DataTable dt = studentbindtypeentitybll.SelectStudentByIsUse(studentbindinfo);
            bool IsBindPaperType = true;      //需要配题
            if (dt.Rows.Count != 0)
            {
                foreach (DataRow dr in dt.Rows)
                {
                    if (dr["IsUse"].ToString() == "1")
                    {
                        IsBindPaperType = false;    //不需要配题
                        MessageBox.Show("该学生已配题成功，请勿重复操作");
                        return;
                    }
                }
            }
           
            //开始配题
            if (IsBindPaperType == true)
            {
               
                //进行配题   ，打上时间戳
                String collegeId;
                try
                {
                    //查询学生信息
                    StudentInfoEntity studentInfo = studentinfoBll.GetStudentById(studentID);
                    collegeId = studentInfo.CollegeID;

                    #region Word配题

                    //WordQuestionEntity中查找出所有的信息。将不同的ABCd等试卷的信息存放在一个list中
                    DataTable worddt = wordquestionbll.WordPaperType();

                    List<List<WordQuestionEntity>> wordquestionlist = new List<List<WordQuestionEntity>>();
                    String Papertype = "";
                    if (worddt.Rows.Count != 0)
                    {
                        for (int i = 0; i < worddt.Rows.Count; i++)
                        {
                            Papertype = worddt.Rows[i]["PaperType"].ToString();
                            wordquestionlist.Add(wordquestionbll.WordPaperTypeGroupByPaperType(Papertype));
                        }

                        //4、给学生分配数据的PaperType,添加WordRecord表             

                        if (examconfigbll.IsTableExist("WordQuestionRecordEntity_" + collegeId) == false)
                        {
                            examconfigbll.CreateDataTableCopySelectRecord("WordQuestionRecordEntity_" + collegeId, "WordQuestionRecordEntity");
                        }

                        List<StudentBindPaperTypeEntity> studentBindPaperTypeEntityList = new List<StudentBindPaperTypeEntity>();      //这里在StudentBindPaperTypeEntity中insert



                        List<WordQuestionRecordEntity> wordrecoredStudentlist = new List<WordQuestionRecordEntity>();

                        String strStudentID = studentInfo.studentID;
                        WordQuestionRecordEntity wordStudent = new WordQuestionRecordEntity();
                        wordStudent.StudentID = strStudentID;

                        int wordi = wordquestionlist.Count;

                        d = Convert.ToInt32(txtPaperType.Text.Trim());

                        if (!(d <= wordi && d >= 1))
                        {
                            MessageBox.Show("没有该数据！");
                            return;
                        }

                        d = d - 1;

                        StudentBindPaperTypeEntity studentBindPaperTypeEntity = new StudentBindPaperTypeEntity();
                        //判断此时是练习模式还是考试模式
                        DataTable entable = new DataTable();
                        entable = logBll.QuerySetUpBll();
                        if (entable.Rows[0]["is_showScore"].ToString() == "0" && entable.Rows[0]["is_upload"].ToString() == "0" && entable.Rows[0]["is_lockScreen"].ToString() == "0")
                        {
                            studentBindPaperTypeEntity.ExamModel = "考试模式";
                        }
                        else
                        {
                            studentBindPaperTypeEntity.ExamModel = "练习模式";
                        }

                        studentBindPaperTypeEntity.StudentID = studentInfo.studentID;
                        studentBindPaperTypeEntity.CollegeID = studentInfo.CollegeID;
                        studentBindPaperTypeEntity.PaperType = wordquestionlist[d][0].PaperType;
                        studentBindPaperTypeEntity.IP = "-";
                        studentBindPaperTypeEntity.IsUse = 1;
                        //studentBindPaperTypeEntity.TimeTamp = timetamp


                        studentBindPaperTypeEntityList.Add(studentBindPaperTypeEntity);

                        studentBindPaperType.InsertRecordList(studentBindPaperTypeEntityList);

                        timetamp = MyInfo.TimeTampTea(studentBindPaperTypeEntity);


                        for (int m = 0; m < wordquestionlist[d].Count; m++)
                        {
                            WordQuestionRecordEntity wordrecoredStudent = new WordQuestionRecordEntity();
                            wordrecoredStudent.StudentID = strStudentID;
                            wordrecoredStudent.QuestionID = wordquestionlist[d][m].QuestionID;
                            wordrecoredStudent.QuestionContent = wordquestionlist[d][m].QuestionContent;
                            wordrecoredStudent.PaperType = wordquestionlist[d][m].PaperType;
                            wordrecoredStudent.RightAnswer = wordquestionlist[d][m].RightAnswer;
                            wordrecoredStudent.TimeStamp = timetamp;
                            wordrecoredStudentlist.Add(wordrecoredStudent);

                        }
                        wordquestionbll.InsertWordRecordList(wordrecoredStudentlist, studentInfo);





                        //-------------------------选择题配题-----------------------------------------------
                        //如果选中全部，则清空所有的选择题答题记录
                        //examConfigBll.ClearSelectQuestionRecordByCollegeID(allCollege)
                        //examconfigbll.ClearSelectQuestionRecordByLstStudent(new List<StudentInfoEntity>() { studentInfo })
                        examconfigbll.RandGenerateRecord(new List<StudentInfoEntity>() { studentInfo }, timetamp);


                    }

                    #endregion

                    #region Window配题

                    //WinQuestionEntity中查找出所有的信息。将不同的ABCd等试卷的信息存放在一个list中
                    DataTable winddt = winquestionbll.WinPaperType();

                    List<List<WinQuestionEntity>> winquestionWhich = new List<List<WinQuestionEntity>>();

                    string WinPapertype = "";

                    if (winddt.Rows.Count != 0)
                    {

                        for (int i = 0; i < winddt.Rows.Count; i++)
                        {
                            WinPapertype = winddt.Rows[i]["paperType"].ToString();
                            winquestionWhich.Add(winquestionbll.WinPaperTypeGroupByPaperType(WinPapertype));
                        }



                        if (examconfigbll.IsTableExist("WinQuestionRecordEntity_" + collegeId) == false)
                        {
                            examconfigbll.CreateDataTableCopySelectRecord("WinQuestionRecordEntity_" + collegeId, "WinQuestionRecordEntity");
                        }





                        //进行分配

                        //做循环
                        List<WinQuestionRecordEntity> winrecoredStudentlist = new List<WinQuestionRecordEntity>();

                        String StrStudentID = studentInfo.studentID;
                        WinQuestionRecordEntity winStudent = new WinQuestionRecordEntity();
                        winStudent.studentID = StrStudentID;

                        //int wini = winquestionWhich.Count
                        //Random ra = new Random()
                        //int d = ra.Next(0, wini - 1)
                        for (int m = 0; m < winquestionWhich[d].Count; m++)
                        {
                            WinQuestionRecordEntity winrecoredStudent = new WinQuestionRecordEntity();
                            winrecoredStudent.studentID = StrStudentID;
                            winrecoredStudent.questionID = winquestionWhich[d][m].questionID;
                            winrecoredStudent.questionContent = winquestionWhich[d][m].questionContent;
                            winrecoredStudent.paperType = winquestionWhich[d][m].paperType;
                            winrecoredStudent.correctAnswer = winquestionWhich[d][m].correctAnswer;
                            winrecoredStudent.timeStamp = timetamp;
                            winrecoredStudentlist.Add(winrecoredStudent);
                        }

                        winquestionbll.InsertWinRecordList(winrecoredStudentlist, studentInfo);



                    }

                    #endregion

                    #region Excel配题

                    //ExcelQuestionEntity中查找出所有的信息。将不同的ABCd等试卷的信息存放在一个list中
                    DataTable exceldt = excelQuestionBll.ExcelPaperType();

                    List<List<ExcelQuestionEntity>> excelquestionlist = new List<List<ExcelQuestionEntity>>();
                    String excelPapertype = "";

                    if (exceldt.Rows.Count != 0)
                    {
                        for (int i = 0; i < exceldt.Rows.Count; i++)
                        {
                            excelPapertype = exceldt.Rows[i]["PaperType"].ToString();
                            excelquestionlist.Add(excelQuestionBll.ExcelPaperTypeGroupByPaperType(excelPapertype));
                        }

                        //4、给学生分配数据的PaperType,添加WordRecord表

                        if (examconfigbll.IsTableExist("ExcelQuestionRecordEntity_" + collegeId) == false)
                        {
                            examconfigbll.CreateDataTableCopySelectRecord("ExcelQuestionRecordEntity_" + collegeId, "ExcelQuestionRecordEntity");
                        }






                        //进行分配

                        List<ExcelQuestionRecordEntity> excelrecoredStudentlist = new List<ExcelQuestionRecordEntity>();

                        String strStudentIDExcel = studentInfo.studentID;
                        ExcelQuestionRecordEntity excelStudent = new ExcelQuestionRecordEntity();
                        excelStudent.StudentID = strStudentIDExcel;


                        //int exceli = excelquestionlist.Count
                        //Random ra = new Random()
                        //int d = ra.Next(0, exceli - 1)
                        for (int m = 0; m < excelquestionlist[d].Count; m++)
                        {
                            ExcelQuestionRecordEntity excelrecoredStudent = new ExcelQuestionRecordEntity();
                            excelrecoredStudent.StudentID = strStudentIDExcel;
                            excelrecoredStudent.QuestionID = excelquestionlist[d][m].QuestionID;
                            excelrecoredStudent.QuestionContent = excelquestionlist[d][m].QuestionContent;
                            excelrecoredStudent.PaperType = excelquestionlist[d][m].PaperType;
                            excelrecoredStudent.CorrectAnswer = excelquestionlist[d][m].CorrectAnswer;
                            excelrecoredStudent.TimeStamp = timetamp;
                            excelrecoredStudentlist.Add(excelrecoredStudent);
                        }
                        excelQuestionBll.InsertExcelRecordList(excelrecoredStudentlist, studentInfo);





                    }

                    #endregion

                    #region IE配题

                    //IEQuestionEntity中查找出所有的信息。将不同的ABCd等试卷的信息存放在一个list中
                    DataTable Ieddt = iequestionbll.WinPaperType();

                    List<List<IEQuestionEntity>> iequestionWhich = new List<List<IEQuestionEntity>>();
                    String iePapertype = "";

                    if (Ieddt.Rows.Count != 0)
                    {

                        for (int i = 0; i < Ieddt.Rows.Count; i++)
                        {
                            iePapertype = Ieddt.Rows[i]["paperType"].ToString();
                            iequestionWhich.Add(iequestionbll.IEPaperTypeGroupByPaperType(iePapertype));
                        }





                        if (examconfigbll.IsTableExist("IEQuestionRecordEntity_" + collegeId) == false)
                        {
                            examconfigbll.CreateDataTableCopySelectRecord("IEQuestionRecordEntity_" + collegeId, "IEQuestionRecordEntity");
                        }





                        //进行分配

                        //做循环
                        List<IEQuestionRecordEntity> ierecoredStudentlist = new List<IEQuestionRecordEntity>();

                        String strStudentIDIE = studentInfo.studentID;
                        IEQuestionRecordEntity IEStudent = new IEQuestionRecordEntity();
                        IEStudent.studentID = strStudentIDIE;


                        //int iei = iequestionWhich.Count
                        //Random ra = new Random()
                        //int d = ra.Next(0, iei - 1)

                        for (int m = 0; m < iequestionWhich[d].Count; m++)
                        {
                            IEQuestionRecordEntity ierecoredStudent = new IEQuestionRecordEntity();
                            ierecoredStudent.studentID = strStudentIDIE;
                            ierecoredStudent.questionID = iequestionWhich[d][m].questionID;
                            ierecoredStudent.questionContent = iequestionWhich[d][m].questionContent;
                            ierecoredStudent.paperType = iequestionWhich[d][m].paperType;
                            ierecoredStudent.correctAnswer = iequestionWhich[d][m].correctAnswer;
                            ierecoredStudent.timeStamp = timetamp;
                            ierecoredStudentlist.Add(ierecoredStudent);
                        }
                        iequestionbll.InsertIERecordList(ierecoredStudentlist, studentInfo);


                    }

                    #endregion

                    #region Ppt配题

                    //PptQuestionEntity中查找出所有的信息。将不同的ABCd等试卷的信息存放在一个list中
                    DataTable pptdt = pptQuestionBll.PptPaperType();

                    List<List<PptQuestionEntity>> pptQuestionlist = new List<List<PptQuestionEntity>>();
                    String PapertypePPT = "";
                    if (pptdt.Rows.Count != 0)
                    {
                        for (int i = 0; i < pptdt.Rows.Count; i++)
                        {
                            PapertypePPT = pptdt.Rows[i]["PaperType"].ToString();
                            pptQuestionlist.Add(pptQuestionBll.PptPaperTypeGroupByPaperType(PapertypePPT));
                        }

                        //4、给学生分配数据的PaperType,添加WordRecord表

                        if (examconfigbll.IsTableExist("PptQuestionRecordEntity_" + collegeId) == false)
                        {
                            examconfigbll.CreateDataTableCopySelectRecord("PptQuestionRecordEntity_" + collegeId, "PptQuestionRecordEntity");
                        }

                        //进行分配

                        List<PptQuestionRecordEntity> pptrecoredStudentlist = new List<PptQuestionRecordEntity>();

                        String strStudentIDPpt = studentInfo.studentID;
                        PptQuestionRecordEntity pptStudent = new PptQuestionRecordEntity();
                        pptStudent.StudentID = strStudentIDPpt;

                        //int ppti = pptQuestionlist.Count
                        //Random ra = new Random()
                        //int d = ra.Next(0, ppti - 1)

                        for (int m = 0; m < pptQuestionlist[d].Count; m++)
                        {
                            PptQuestionRecordEntity pptrecoredStudent = new PptQuestionRecordEntity();
                            pptrecoredStudent.StudentID = strStudentIDPpt;
                            pptrecoredStudent.QuestionID = pptQuestionlist[d][m].QuestionID;
                            pptrecoredStudent.QuestionContent = pptQuestionlist[d][m].QuestionContent;
                            pptrecoredStudent.PaperType = pptQuestionlist[d][m].PaperType;
                            pptrecoredStudent.RightAnswer = pptQuestionlist[d][m].RightAnswer;
                            pptrecoredStudent.TimeStamp = timetamp;
                            pptrecoredStudentlist.Add(pptrecoredStudent);
                        }
                        pptQuestionBll.InsertPptRecordList(pptrecoredStudentlist, studentInfo);

                    }

                    #endregion

                    MessageBox.Show(this, "配题成功！");
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }

            }

            #region 将考生文件从C盘拷贝到D盘-韩梦甜-2015-12-25

            string Cpath = "C:\\计算机一级考生文件";
            string Dpath = "D:\\";
            if (!CopyFolder.Copy(Cpath, Dpath))
            {

                MessageBox.Show("拷贝失败！");
            }
            #endregion
        } 
        #endregion

        #region 文本框限制输入-苑仁杰-2018-12-06
        /// <summary>
        /// 输入学号文本框
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void SingleStudentConfigBox_KeyPress(object sender, KeyPressEventArgs e)
        {
            //文本框限制只能输入数字和字母
            if ((e.KeyChar >= 'A' && e.KeyChar <= 'Z')|| (e.KeyChar >= '0' && e.KeyChar <= '9') || (e.KeyChar == 8))
            {
                e.Handled = false;
            }
            else
            {
                e.Handled = true;
            }
        }
        /// <summary>
        /// 输入试题文本框
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void txtPaperType_KeyPress(object sender, KeyPressEventArgs e)
        {
            //文本框限制只能输入数字和字母、退格键
            if ((e.KeyChar >= 'A' && e.KeyChar <= 'Z') || (e.KeyChar >= '0' && e.KeyChar <= '9') || (e.KeyChar == 8))
            {
                e.Handled = false;
            }
            else
            {
                e.Handled = true;
            }
        }
        #endregion

        #region 窗体加载清空事件-郑璐璐-2018年12月12日14:18:42
        /// <summary>
        /// 窗体加载
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void frmSingleStu_Load(object sender, EventArgs e)
        {
            //窗体加载时,清空文本框内容,当窗体第二次打开时,不会有上次显示的内容
            SingleStudentConfigBox.Text = "";
            txtPaperType.Text = "";
        }
        #endregion
    }
}
